package com.feng.freader.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.feng.freader.R;
import com.feng.freader.adapter.CatalogAdapter;
import com.feng.freader.base.BaseActivity;
import com.feng.freader.base.BasePresenter;
import com.feng.freader.constant.EventBusCode;
import com.feng.freader.entity.epub.EpubTocItem;
import com.feng.freader.entity.epub.OpfData;
import com.feng.freader.entity.eventbus.EpubCatalogInitEvent;
import com.feng.freader.entity.eventbus.Event;
import com.feng.freader.util.StatusBarUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class AdActivity extends BaseActivity implements View.OnClickListener{

    private ImageView mBackIv;
    private Button btn_1;
    private Button btn_2;
    private Button btn_3;
    private Button btn_4;

    private TTRewardVideoAd mTTRewardVideoAd; // 插全屏广告对象
    private TTAdNative.RewardVideoAdListener mRewardVideoListener; // 广告加载监听器
    private TTRewardVideoAd.RewardAdInteractionListener mRewardVideoAdInteractionListener; // 广告展示监听器

    // 通过 EventBus 初始化
    private ReadActivity mReadActivity;
    private OpfData mOpfData;
    private String mNovelUrl;
    private String mName;
    private String mCover;

    @Override
    protected void doBeforeSetContentView() {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);   //隐藏标题栏
    }

    @Override
    protected int getLayoutId() {
        return R.layout.ad;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        mBackIv = findViewById(R.id.iv_epub_catalog_back);
        mBackIv.setOnClickListener(this);
        btn_1 = findViewById(R.id.button);
        btn_1.setOnClickListener(this);
        btn_2 = findViewById(R.id.button2);
        btn_2.setOnClickListener(this);
        btn_3 = findViewById(R.id.button3);
        btn_3.setOnClickListener(this);
    }

    @Override
    protected void doAfterInit() {
        StatusBarUtil.setLightColorStatusBar(this);
        getWindow().setStatusBarColor(getResources().getColor(R.color.catalog_bg));
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onStickyEventBusCome(Event event) {
        switch (event.getCode()) {
            case EventBusCode.EPUB_CATALOG_INIT:

                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_epub_catalog_back:
                finish();
                break;
            case R.id.button:
                loadAd();
                showAd();
                break;
            case R.id.button2:
                Intent intent = new Intent(this, MediationSplashActivity.class);
                startActivity(intent);
                break;
            case R.id.button3:
                Intent intent3 = new Intent(this, MediationBannerActivity.class);
                startActivity(intent3);
                break;
            default:
                break;
        }
    }

    private void showAd() {
        if (mTTRewardVideoAd == null) {
            Log.i("AD", "请先加载广告,或等待广告加载完毕后再调用show方法");
            return;
        }
        /** 5、设置展示监听器，展示广告 */

        mTTRewardVideoAd.setRewardAdInteractionListener(mRewardVideoAdInteractionListener);
        mTTRewardVideoAd.showRewardVideoAd(this);
    }
    private void loadAd() {
        Log.i("AD", "loadAd...................");

        AdSlot adslot = new AdSlot.Builder()
//                .setCodeId("957974053")
                .setCodeId("958210175")
                .setOrientation(TTAdConstant.ORIENTATION_VERTICAL)
                .build();
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(this);//需要传activity，切记！！！
        initListeners();
        adNativeLoader.loadRewardVideoAd(adslot, mRewardVideoListener);

    }

    private void initListeners() {
        // 广告加载监听器
        this.mRewardVideoListener = new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int i, String s) {
                Log.i("Ad", "reward load fail: errCode: " + i + ", errMsg: " + s);
            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {
                Log.i("Ad", "reward load success");
                mTTRewardVideoAd = ttRewardVideoAd;
            }

            @Override
            public void onRewardVideoCached() {
                Log.i("Ad", "reward cached success");
            }

            @Override
            public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
                Log.i("Ad", "reward cached success 2");
                mTTRewardVideoAd = ttRewardVideoAd;
            }
        };
        // 广告展示监听器

        this.mRewardVideoAdInteractionListener = new TTRewardVideoAd.RewardAdInteractionListener() {
            @Override
            public void onAdShow() {
                Log.i("Ad", "reward show");
            }

            @Override
            public void onAdVideoBarClick() {
                Log.i("Ad", "reward click");
            }

            @Override
            public void onAdClose() {
                Log.i("Ad", "reward close");
            }

            @Override
            public void onVideoComplete() {
                Log.i("Ad", "reward onVideoComplete");
            }

            @Override
            public void onVideoError() {
                Log.i("Ad", "reward onVideoError");
            }

            @Override
            public void onRewardVerify(boolean b, int i, String s, int i1, String s1) {
                Log.i("Ad", "reward onRewardVerify");
            }

            @Override
            public void onRewardArrived(boolean isRewardValid, int rewardType, Bundle extraInfo) {
                Log.i("Ad", "reward onRewardArrived");
            }

            @Override
            public void onSkippedVideo() {
                Log.i("Ad", "reward onSkippedVideo");
            }
        };
    }

}
